const canvas = wx.createCanvas()
const { windowWidth, windowHeight } = wx.getSystemInfoSync()
const context = canvas.getContext('2d') // 创建一个 2d context
context.fillStyle = 'lightblue'
const bgm = wx.createInnerAudioContext()
bgm.src = './audio/bgm.mp3'
bgm.autoplay = true
bgm.loop = true
bgm.onCanplay = function(){
  bgm.play
}
const audioA = wx.createInnerAudioContext()
audioA.src = './audio/a.mp3'
audioA.autoplay = false
const audioB = wx.createInnerAudioContext()
audioB.src = './audio/b.mp3'
audioB.autoplay = false
const imageA = wx.createImage()
const imageB = wx.createImage()
const bg = wx.createImage()
imageA.src = 'image/a.png'
imageB.src = 'image/b.png'
let imgAX,imgAY,imgBX,imgBY
imageA.onload = function () {
  imgAX = (canvas.width - imageA.width) / 2
  imgAY = 0
  context.drawImage(imageA, imgAX, imgAY)
}
imageB.onload = function () {
  imgBX = (canvas.width - imageB.width) / 2
  imgBY = canvas.height/2
  context.drawImage(imageB, imgBX, imgBY)
}

function clear(){
  context.fillRect(0, 0, windowWidth, windowHeight)
}

function draw(){
  context.drawImage(imageA, imgAX, imgAY)
  context.drawImage(imageB, imgBX, imgBY)
}

wx.onTouchStart(function (res) {
  let touchX = res.changedTouches[0].clientX // 重新判断当前触摸点x坐标
  let touchY = res.changedTouches[0].clientY // 重新判断当前触摸点y坐标
  if(touchY > (windowHeight / 2)){
    audioA.stop()
    audioB.stop()
    audioA.play()
  }else{
    audioA.stop()
    audioB.stop()
    audioB.play()
  }
})

function start(){
  clear()
  draw()
  setTimeout(start,20)
}
start()